package com.gadgets;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-01-20
 */
@Configuration
@MapperScan(value = "com.gadgets.**.repository", sqlSessionFactoryRef = "gadgetsDbSessionFactory")
public class GadgetsDbConf {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${gadgets.datasource.url}")
    private String gadgetDbUrl;
    @Value("${gadgets.datasource.username}")
    private String gadgetDbUserName;
    @Value("${gadgets.datasource.password}")
    private String gadgetDbPassword;
    @Value("${gadgets.datasource.driverClassName}")
    private String gadgetDbDriverClass;
    @Value("${gadgets.datasource.initialSize}")
    private int gadgetsInitialSize;
    @Value("${gadgets.datasource.minIdle}")
    private int gadgetsMinIdle;
    @Value("${gadgets.datasource.maxActive}")
    private int gadgetsMaxActive;
    @Value("${gadgets.datasource.maxWait}")
    private int gadgetsMaxWait;
    @Value("${gadgets.datasource.timeBetweenEvictionRunsMillis}")
    private int gadgetsTimeBetweenEvictionRunsMillis;
    @Value("${gadgets.datasource.minEvictableIdleTimeMillis}")
    private int gadgetsMinEvictableIdleTimeMillis;
    @Value("${gadgets.datasource.testWhileIdle}")
    private boolean gadgetsTestWhileIdle;
    @Value("${gadgets.datasource.testOnBorrow}")
    private boolean gadgetsTestOnBorrow;
    @Value("${gadgets.datasource.testOnReturn}")
    private boolean gadgetsTestOnReturn;
    @Value("${gadgets.datasource.keepAlive}")
    private boolean gadgetsKeepAlive;
    @Value("${gadgets.datasource.poolPreparedStatements}")
    private boolean gadgetsPoolPreparedStatements;
    @Value("${gadgets.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int gadgetsMaxPoolPreparedStatementPerConnectionSize;
    @Value("${gadgets.datasource.filters}")
    private String gadgetsFilters;
    @Value("${gadgets.mapperLocations}")
    private String gadgetsDbMapper;


    @Bean(name = "gadgetsDbSource")
    @Primary
    public DataSource gadgetsDataSource() throws SQLException {
        logger.info("注册gadgets数据源");
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(gadgetDbDriverClass);
        druidDataSource.setUrl(gadgetDbUrl);
        druidDataSource.setUsername(gadgetDbUserName);
        druidDataSource.setPassword(gadgetDbPassword);
        druidDataSource.setInitialSize(gadgetsInitialSize);
        druidDataSource.setMinIdle(gadgetsMinIdle);
        druidDataSource.setMaxActive(gadgetsMaxActive);
        druidDataSource.setMaxWait(gadgetsMaxWait);
        druidDataSource.setTimeBetweenEvictionRunsMillis(gadgetsTimeBetweenEvictionRunsMillis);
        druidDataSource.setMinEvictableIdleTimeMillis(gadgetsMinEvictableIdleTimeMillis);
        druidDataSource.setTestWhileIdle(gadgetsTestWhileIdle);
        druidDataSource.setTestOnBorrow(gadgetsTestOnBorrow);
        druidDataSource.setTestOnReturn(gadgetsTestOnReturn);
        druidDataSource.setKeepAlive(gadgetsKeepAlive);
        druidDataSource.setPoolPreparedStatements(gadgetsPoolPreparedStatements);
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(gadgetsMaxPoolPreparedStatementPerConnectionSize);
        druidDataSource.setFilters(gadgetsFilters);
        logger.info("数据库连接：{}", gadgetDbUrl);
        return druidDataSource;
    }

    @Bean(name = "gadgetsDbTransactionManage")
    public DataSourceTransactionManager gadgetsDataSourceTransactionManager(@Qualifier("gadgetsDbSource") DataSource masterDataSource){
        return new DataSourceTransactionManager(masterDataSource);
    }


    @Bean(name = "gadgetsDbSessionFactory")
    @Primary
    public SqlSessionFactory gadgetsDataSourceSessionFactory(@Qualifier("gadgetsDbSource") DataSource masterDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(masterDataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(gadgetsDbMapper));
        return sessionFactoryBean.getObject();
    }

}
